<?

//Initiate Authenticate
SecurityRedirect ();

//initiate Vars
define("PRIMARY_KEY","skada_id");
$KEY[]         = array("PRIMARY_KEY"=>PRIMARY_KEY);
$FileName      = "ais_setup_budget_usd.php";
$FileTemplate  = "ais_setup_budget_usd_list.html";
$TableName     = "tblcoa";
$SubHeaderTitle=  "Setup Budget In USD List";
$PageSize      = 10;
$HTMLGridList  = "ReceiptGrid";

$FormFields    = array("m_1_"=>"number",
					   "m_2_"=>"number",
					   "m_3_"=>"number",
					   "m_4_"=>"number",
					   "m_5_"=>"number",
					   "m_6_"=>"number",
					   "m_7_"=>"number",
					   "m_8_"=>"number",
					   "m_9_"=>"number",
					   "m_10_"=>"number",
					   "m_11_"=>"number",
					   "m_12_"=>"number");

//Initiate Database Table For Search;
$FDesc = GetParam("descriptionSearch","");
$FSkada = GetParam("skadaSearch","");
//$FDept = GetParam("deptSearch","");

//******* DATABASE FORM QUERY ***********
$SQL = "SELECT * FROM `$TableName` ";
$SQLCount = "SELECT count(*) FROM `$TableName` ";
$WHERE = "tipe=0 AND ktrasl='".GetOfficeID()."'";

//Retrieve Parameter from search
if ($FSkada) {
	$CustomWhere = " skada like '".$FSkada."%'";
	if (strlen($WHERE)) $WHERE .= " AND ".$CustomWhere;
	else $WHERE = $CustomWhere;	
}

if ($FDesc) {
	$CustomWhere = " $TableName.namacc like '".$FDesc."%'";
	if (strlen($WHERE)) $WHERE .= " AND ".$CustomWhere;
	else $WHERE = $CustomWhere;
}

if (strlen($WHERE)) { 
	$SQL = $SQL." WHERE ".$WHERE;
	$SQLCount = $SQLCount." WHERE ".$WHERE;
} else $SQL = $SQL;

//Select Department 
$SQLDept = "SELECT * FROM tbldept";
//Masking

//******* SHOW FORM CONTENT *********
$PageNum = 1;
$RecCnt = $DBConnection->dbc->get_var($SQLCount);

if (GetParam("PageNum","")) $PageNum  = GetParam("PageNum","");
//print $SQL;
//************ FORM ACTION **************
if (GetParam("Action","")=="Save") {
        //print "SaveRecord($PageNum,$RecCnt);";die();
	SaveRecord($PageNum,$RecCnt);
}

$TBS = new clsTinyButStrong ;
$TBS->LoadTemplate($MOD_TEMPLATE_DIR."/".$FileTemplate);
$TBS->MergeBlock("bln",$KEY);
$TBS->MergeBlock("blg",$DBMysql,$SQL,$PageSize,$PageNum,$RecCnt);
$TBS->MergeBlock("blg1",$DBMysql,$SQL,$PageSize,$PageNum,$RecCnt);
$TBS->MergeBlock("deptblk",$DBMysql,$SQLDept);
$TBS->MergeNavigationBar('nv',array('size'=>10,'pos'=>'centred'),$PageNum,$RecCnt,$PageSize) ; 
$TBS->Show();


function SaveRecord ($PageNumber,$TotalRec) {
	global $PageSize, $DBConnection;
        if ($PageNumber) {
          $Start = $PageNumber*$PageSize-$PageSize+1;
          $Stop   = $Start+$PageSize-1;
        } 

	for ($x=$Start;$x<=$Stop;$x++) {
		$SQL  = "UPDATE tblcoa SET bgtd01='".StrToNumber(GetParam("m_1_".$x,""))."', bgtd02='".StrToNumber(GetParam("m_2_".$x,""))."', bgtd03='".StrToNumber(GetParam("m_3_".$x,""))."', bgtd04='".StrToNumber(GetParam("m_4_".$x,""))."', bgtd05='".StrToNumber(GetParam("m_5_".$x,""))."', bgtd06='".StrToNumber(GetParam("m_6_".$x,""))."', bgtd07='".StrToNumber(GetParam("m_7_".$x,""))."', bgtd08='".StrToNumber(GetParam("m_8_".$x,""))."', bgtd09='".StrToNumber(GetParam("m_9_".$x,""))."', bgtd10='".StrToNumber(GetParam("m_10_".$x,""))."', bgtd11='".StrToNumber(GetParam("m_11_".$x,""))."', bgtd12='".StrToNumber(GetParam("m_12_".$x,""))."' ";
		$SQL .= "WHERE skada_id='".GetParam("m_skada".$x,"")."'";
		$DBConnection->dbc->query($SQL);
	}
	//Execute Update to Sysmon DB
}


function DeleteRecord ($sequence_name,$PageSize) {
	global $DBConnection,$FileName,$TableName;	
	for ($x=1;$x<=$PageSize;$x++) {				
		if (GetParam($sequence_name.$x,""))	{
			$DBConnection->Delete("$TableName",$sequence_name.$x);
	    }
	}
	header("Location: $FileName");
	exit;	
	
}

function MutatorFields ($FormFields, $MaxGrid) {
		global $DBConnection;
		$array_fields = array();
		for ($x=0;$x<$MaxGrid;$x++) {
			foreach ($FormFields as $key=>$formatting){
				$a=$x+1;
				$array_fields[$key.$a] = "$formatting";
			}			
		}
		return $array_fields;
}
?>
